//
//  RegisterViewModel.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "RegisterViewModel.h"
#import <AFNetworking.h>

@implementation RegisterViewModel

+ (void)registerByPhone:(NSString *)phone
               password:(NSString *)password
             verifycode:(NSString *)verifycode
                success:(void (^)())success
                failure:(void (^)(NSString *))failure{
    
    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/passport/register/byphone"];
    
    NSString *post = [NSString stringWithFormat:@"verifycode=%@&pwd=%@&phone=%@",verifycode,password,phone];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    
  
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
                               }else{
                                   NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                               error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                       success();
                                       
                                       
                                   }else{
                                       
                                       NSString *messge = [responseDic objectForKey:@"message"];
                                       
                                       failure(messge);
                                   }
                               }
                           }];
    
}

+ (void)verifyCodeByPhone:(NSString *)phone
                  success:(void (^)())success
                  failure:(void (^)(NSString *))failure{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/passport/captcha/send"
      parameters:@{@"phone":phone}
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *responseDic=[NSJSONSerialization
                                        JSONObjectWithData:responseObject
                                        options:NSJSONReadingMutableLeaves
                                        error:nil];
             if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                 success();
             }else{
                 failure([responseDic objectForKey:@"message"]);
                 
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             failure(@"网络连接失败");
         }];
    



    
}
@end
